<?php

namespace app\api\controller\department;

use app\BaseController;
use app\common\validate\department\DepartmentPosition as ValidateModel;
use app\common\business\department\DepartmentPosition as serviceModel;
use think\exception\ValidateException;

class DepartmentPosition extends BaseController 
{ 

    /* 
     *【方法注释】获取数据列表 
     */
    public function list(){
        $fildes = 'page';
        $receiveData = getData($this->request->post(),$fildes);
        $where = [];
        $where[] = ['status','in',[1]];
        $limit_start = ($receiveData['page']-1) * env('api_page_size');
        $lists = serviceModel::getList($where,$limit_start,env('page_size'));
        $data = [
            'lists' => $lists,
            'is_load' => isLoad(count($lists),env('api_page_size'))
        ];
        jsonSuccess($data);
    } 

    /* 
     *【方法注释】修改和编辑 
     */
    public function edit(){
        $fildes = 'id,position_name,code_no,status,department_id,order_num,remark,admin_name';
        $receiveData = getData($this->request->post(),$fildes);
        try{
            if(!empty($receiveData['id'])){
                validate(validateModel::class)->scene('edit')->check($receiveData);
            }else{
                validate(validateModel::class)->scene('add')->check($receiveData);
            }
        } catch (ValidateException $e) {
            jsonFail($e->getError());
        }
        if(!empty($receiveData['id'])){
            serviceModel::edit($receiveData,true);
        }else{
            serviceModel::insert($receiveData,"",true);
        }
        jsonSuccess();
    } 

    /* 
     *【方法注释】删除数据 
     */
    public function delete(){
        $fildes = 'id';
        $receiveData = getData($this->request->post(),$fildes);
        $where = [];
        $where[] = ['id','=',$receiveData['id']];
        $where[] = ['status','in',[1]];
        $load = serviceModel::getOne($where);
        if(empty($load)){
            jsonFail('数据不存在');
        }
        serviceModel::updateStatus(-1,$load);
        jsonSuccess();
    } 

    /* 
     *【方法注释】获取数据列表 
     */
    public function load(){
        $fildes = 'id';
        $receiveData = getData($this->request->post(),$fildes);
        $where = [];
        $where[] = ['status','in',[1]];
        $where[] = ['id','=',$receiveData['id']];
        $data = [
            'model' => serviceModel::getOne($where),
        ];
        jsonSuccess($data);
    } 

} 
